#!/usr/bin/ruby -Ku
# -*- coding: utf-8 -*-
require 'test/unit'
require 'm3g'

class Test_Node < Test::Unit::TestCase
  def setup
  end

  def teadown
  end

  def test_defualt_variables
    node = M3G::Group.new

    assert_equal nil , node.parent
    assert_equal true, node.rendering_enabled?
    assert_equal true, node.picking_enabled?
    assert_equal 1.0 , node.alpha_factor
    assert_equal -1  , node.scope
    assert_equal M3G::Node::NONE, node.alignment[M3G::Node::Z_AXIS].target
    assert_equal nil            , node.alignment[M3G::Node::Z_AXIS].reference
    assert_equal M3G::Node::NONE, node.alignment[M3G::Node::Y_AXIS].target
    assert_equal nil            , node.alignment[M3G::Node::Y_AXIS].reference
  end

  def test_set_variables
    node = M3G::Group.new
    zref = M3G::Group.new
    yref = M3G::Group.new
    grp  = M3G::Group.new
    wld  = M3G::World.new
    node.alignment        = [zref, M3G::Node::ORIGIN, yref, M3G::Node::ORIGIN]
    node.alpha_factor     = 0.5
    node.rendering_enable = false
    node.picking_enable   = false
    node.scope            = 1
    grp.child << node;
    wld.child << grp;

    assert_equal grp  , node.parent
    assert_equal wld  , grp.parent
    assert_equal false, node.rendering_enabled?
    assert_equal false, node.picking_enabled?
    assert_equal 0.5  , node.alpha_factor
    assert_equal 1    , node.scope
    assert_equal M3G::Node::ORIGIN, node.alignment[M3G::Node::Z_AXIS].target
    assert_equal zref             , node.alignment[M3G::Node::Z_AXIS].reference
    assert_equal M3G::Node::ORIGIN, node.alignment[M3G::Node::Y_AXIS].target
    assert_equal yref             , node.alignment[M3G::Node::Y_AXIS].reference
  end

  def test_dupliate
    node0  = M3G::Group.new
    zref   = M3G::Group.new
    yref   = M3G::Group.new
    parent = M3G::Group.new
    grp    = M3G::Group.new

    node0.alignment = [zref, M3G::Node::ORIGIN, yref, M3G::Node::ORIGIN];
    node0.alpha_factor = 0.5
    node0.rendering_enable = false;
    node0.picking_enable   = false;
    node0.scope            = 1;

    # node0のparentがセットされる
    grp.child << node0;

    node1 = node0.duplicate

    # duplicate()したNodeのparentはNULL！
    assert_equal nil, node1.parent

    assert_equal node1.rendering_enabled?, node0.rendering_enabled?
    assert_equal node1.picking_enabled?  , node0.picking_enabled?
    assert_equal node1.alpha_factor      , node0.alpha_factor
    assert_equal node1.scope             , node0.scope
    assert_equal node1.alignment[M3G::Node::Z_AXIS].target   , node0.alignment[M3G::Node::Z_AXIS].target
    assert_equal node1.alignment[M3G::Node::Z_AXIS].reference, node0.alignment[M3G::Node::Z_AXIS].reference
    assert_equal node1.alignment[M3G::Node::Y_AXIS].target   , node0.alignment[M3G::Node::Y_AXIS].target
    assert_equal node1.alignment[M3G::Node::Y_AXIS].reference, node0.alignment[M3G::Node::Y_AXIS].reference
  end

  def test_transform_to
    node_a = M3G::Group.new
    node_a.translation = [1,0,0]

    node_b = M3G::Group.new
    node_b.translation = [0,1,0]
  
  
    grp0 = M3G::Group.new;
    grp0.translation = [0,0,2]
    grp0.scaling     = [1,2,3]

    grp0.child << node_a
    grp0.child << node_b

    calculated, trans = node_a.transform_to node_b

    assert_equal true, calculated
    assert_instance_of M3G::Transform, trans
    matrix = [1,0,0,1,
              0,1,0,-1,
              0,0,1,0,
              0,0,0,1]
    for i in 0..15
      assert_in_delta matrix[i], trans.matrix[i], 0.00001
    end
  end

  def test_align 
    zref = M3G::Group.new
    zref.orientation = [45, 0,0,1]

    yref = M3G::Group.new
    yref.orientation = [45, 0,1,0]

    node = M3G::Group.new
    node.alignment = [zref, M3G::Node::ORIGIN, yref, M3G::Node::ORIGIN]
    
    node.align nil

    # TODO: テスト不十分。
    # 後で追加する。
  end
end
